package dao;

import dao.Entity.TimeTable;
import dao.Entity.Time;
import dao.Entity.Data;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.alibaba.fastjson.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class QueryTimeTable {

    public static String getDates() {
        JSONArray jsonArray = new JSONArray();
        MyConnection myCon = new MyConnection();
        myCon.getConnection();
        try {
            PreparedStatement ps = myCon.connection.prepareStatement("select placeDate from placedate ORDER BY PlaceDate");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                jsonArray.add(rs.getString(1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myCon.closeConnection();
        }
        return jsonArray.toJSONString();
    }

    public static String getResult(String PlaceNumber, String PlaceDate) throws ParseException {
        return getDailyData(getTimeTable(PlaceNumber, PlaceDate));
    }

    public static String getDailyData(TimeTable res) throws ParseException {
        SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss");
        ArrayList<String> times = new ArrayList<>();
        times.add("00:00:00");
        String temp = "00:00:00";
        String zerotime = "00:00:00";
        String temp_end = "24:00:00";
        boolean flag = true;

        for (int i = 0; i < res.timeTable.size(); i++) {
            if (i != 0) {
                if (!res.timeTable.get(i).startTime.equals(temp_end)) {
                    times.add(res.timeTable.get(i - 1).endTime);
                }
            }
            if (res.timeTable.get(i).startTime.equals(zerotime)) {
                flag = false;
            }
            if (!res.timeTable.get(i).startTime.equals(temp)) {
                times.add(res.timeTable.get(i).startTime);
            }
            temp = res.timeTable.get(i).endTime;
            temp_end = res.timeTable.get(i).endTime;
        }
        times.add(res.timeTable.get(res.timeTable.size() - 1).endTime);
        if (!temp_end.equals("24:00:00")) {
            times.add("24:00:00");
        }
        Data[] datas = new Data[times.size() - 1];
        for (int i = 0; i < datas.length; i++) {
            datas[i] = new Data((int) (ft.parse(times.get(i + 1)).getTime() - ft.parse(times.get(i)).getTime()) / 1000, times.get(i) + "~" + times.get(i + 1));
        }

        JSONArray timeArray = new JSONArray();
        for (int i = 0; i < datas.length; i++) {
            timeArray.add(i, datas[i]);
        }
        JSONObject time = new JSONObject();
        time.put("times", timeArray);
        time.put("status", flag);
        return time.toJSONString();

    }

    public static TimeTable getTimeTable(String PlaceNumber, String PlaceDate) {
        TimeTable res = null;
        MyConnection myCon = new MyConnection();
        myCon.getConnection();
        try {
            PreparedStatement ps = myCon.connection.prepareStatement("SELECT PlaceStartTime,PlaceEndTime FROM placetime where PlaceNumber=? and PlaceDate=? ORDER BY PlaceStartTime;");
            ps.setInt(1, Integer.parseInt(PlaceNumber));
            ps.setString(2, PlaceDate);
            ResultSet rs = ps.executeQuery();
            res = new TimeTable(PlaceDate);
            while (rs.next()) {
                res.appendTime(new Time(rs.getString(1), rs.getString(2)));
            }
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myCon.closeConnection();
        }
        return res;
    }
}
